//
//  PhotoBrowserCell.swift
//  SinaWeibo
//
//  Created by NeXTSTEP on 15/10/4.
//  Copyright © 2015年 NeXTSTEP. All rights reserved.
//

import UIKit
import SDWebImage


class PhotoBrowserCell: UICollectionViewCell {

    
    var imageURL: NSURL? {
        didSet {
            indicator.startAnimating()
            
            //重设scrollView
            resetScrollView()
            imageView.sd_setImageWithURL(imageURL!) { (image, _, _, _) -> Void in
                self.indicator.stopAnimating()
                if image == nil {
                    print("下载图像错误")
                    return
                }
                
//                let size = image.size
//                self.imageView.sizeToFit()
                self.setupImagePostion()
            }
        }
    }
    
    private func resetScrollView(){
        imageView.transform = CGAffineTransformIdentity
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }
    
    //设置图像位置  
    ///提示长短图的概念在照片查看器中非常重要
    private func setupImagePostion(){
        let s = disPlaySize(imageView.image!)
        
        if s.height < scrollView.bounds.height {
            //短图片  垂直居中
            let y = (scrollView.bounds.height - s.height) * 0.5
            imageView.frame = CGRect(origin: CGPointZero, size: s)
            
            
//            imageView.frame = CGRect(x: 0, y: y, width: s.width, height: s.height)
            scrollView.contentInset  = UIEdgeInsetsMake(y, 0, y, 0)
        } else {
            //长图
            imageView.frame = CGRect(origin: CGPointZero, size: s)
            scrollView.contentSize = s
        }
        
    }
    
    private func disPlaySize(image: UIImage) -> CGSize {
        let scale = image.size.height / image.size.width
        let height = scale * scrollView.bounds.width
        
        return CGSize(width: scrollView.bounds.width, height: height)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        contentView.addSubview(indicator)
        indicator.center = scrollView.center
    }
    
    
    //懒加载
    private lazy var scrollView: UIScrollView = {
        let sv = UIScrollView(frame: UIScreen.mainScreen().bounds)
        sv.minimumZoomScale = 0.5
        sv.maximumZoomScale = 2.0
        sv.delegate = self
        return sv
    }()
    
    lazy var imageView: UIImageView = UIImageView()
    private lazy var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
}

extension PhotoBrowserCell : UIScrollViewDelegate {

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        //告诉协议 需要缩放的控件
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        //a,d 表示的是缩放比例
        //tx, ty 位移
        //adcd 共同决定旋转
        print("scale = \(scale)")
        print("transform = \(view!.transform)")
        
        ///重新计算frame
        let offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5

        let offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
//        imageView.frame = CGRectMake(offsetX, offsetY, view!.frame.width, view!.frame.height)
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
        
    }
    

}

